package com.topfinance.frame.util;

import java.io.File;
import java.util.HashMap;

import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 * 
 * @author 陈志铉
 * @version 1
 * @
 */
public class BeanManager {
	private static final Log log = LogFactory.getLog(BeanManager.class);
	private boolean test = false;
	private final static String DATA_SOURCE_NAME_KEY="dataSource";//in xml, mz
	public static String APP_PATH=""; //end with '/', looks like d:/dir1/dir2/
	//存放MOCK对象
	private HashMap<String,Object> map = null;
	
	public void setTest(){
		test = true;
		map = new HashMap<String,Object>() ;
	}
	
	public boolean isTest(){
		return test;
	}
	
	public void addMock(Class targetClas,Object obj){
		if(test){
			String serviceid = targetClas.getName().trim();			
			addMock(serviceid,obj);
		}
	}
	
	public void addMock(String name,Object obj){					
		if(test){
			map.put(name, obj);
		}
	}
	
	private BeanManager() {
		//Timer timer = new Timer(true);
//		timer.schedule(new ValidateLicense(), 0, DateUtil.ONE_DAY);
	}

	private static ApplicationContext appContext = null;
	
	static {
		//取得classes下面所有的beans.xml文件
		String path =  Thread.currentThread().getContextClassLoader().getResource("").toString().trim();
		//path looks like "file:/D:/workspace/servicemonitorsys/WebRoot/WEB-INF/classes/"
//		path  = path.substring(0, path.lastIndexOf("/"));
//		path  = path.substring(0, path.lastIndexOf("/"));	
//		path  = path.substring(0, path.lastIndexOf("/")+1);
		APP_PATH = path.substring(6,path.indexOf("WEB-INF"));
		//if unix, needs to plus '/'
		if(File.separator.equals("/")){
			APP_PATH = "/" + APP_PATH;
		}
//		appContext = new FileSystemXmlApplicationContext(path+"spring/*.xml");
		appContext = new ClassPathXmlApplicationContext("/spring/*.xml");
	}

	private static BeanManager instance = new BeanManager();

	public static BeanManager getInstance() {
		return instance;
	}

	
	public DataSource getDataSource(){
//		return (DataSource)appContext.getBean(DataSource.class.getName().trim());
		return (DataSource)appContext.getBean(DATA_SOURCE_NAME_KEY);
		
	}
	//由于可能有本地调用，事务需要合并,所以将事务处理放到外部
	public Object getService(Class targetClass) {		
		String serviceid = targetClass.getName().trim();			
		return getService(serviceid);
	}
	
	public Object getService(String name) {
	
		if(!test){
			Object bean = appContext.getBean(name);
			return bean;
		}else{
				Object bean = map.get(name);
				if(bean == null){
					bean = appContext.getBean(name);
				}
				return bean;
			
		}

	}
	public static void main(String[] args){
//		ITestManager o = (ITestManager)BeanManager.getInstance().getService("testManager");
//		IBizProManager pro = (IBizProManager)BeanManager.getInstance().getService("bizProManager");
//		try {
////			pro.find("WF1",new PageReq());
////			Page page = o.find(null);
////			JSONArray jsonObject = JSONArray.fromObject( page.getList() );   
////			System.out.println( jsonObject );  
////			List list = JSONArray.toList(jsonObject, HashMap.class);
////			System.out.println( list );
////			HashMap map = new HashMap();
////			map.put("id", 1);
////			map.put("name", "name3333");
////			List list2 = new ArrayList();
////			list2.add(map);
////			System.out.println( list2 );
//		} catch (AppException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
	}
	
}
